<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/11
 * Time: 16:53
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class CardExtendParameter extends BaseValidate
{
    protected $rule=[
        'synopsis'=>'require',
        'audio'=>'checkAudio',
        'label'=>'require|checkLabels',
    ];

    protected $singleRule=[
        'audio_id'=>'isPositiveInt',
    ];
    /**
     * 验证数组
     * @param $values
     * @return bool
     * @throws ParameterException
     */
    public function checkLabels($values){
        if (empty($values)){
            throw new ParameterException([
                'msg'=>'标签不能为空'
            ]);
        }
        if (!is_array($values)){
            throw new ParameterException([
                'msg'=>'标签参数不正确'
            ]);
        }
        foreach ($values as $value){
            if(count($value)>8){
                throw new ParameterException([
                    'msg'=>'超出最大标签长度'
                ]);
            }
        }
        return true;
    }
    /**
     * 验证数组里面的每个元素
     * @param $product
     * @return bool
     * @throws ParameterException
     */
    public function checkAudio($value){
        $validate  = new BaseValidate($this->singleRule);
        $result = $validate->check($value);
        if (!$result){
            throw new ParameterException([
                'msg'=>'音频上传参数不正确'
            ]);
        }
        return true;
    }
}